package com.ys.peony.ui.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.app.HttpURL;
import com.ys.peony.base.BaseActivity;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespBean;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.model.BusiLoanModel;
import com.ys.peony.model.ClientInfoModel;
import com.ys.peony.model.VehicleModel;
import com.ys.peony.model.YWDJPrepareItemModel;
import com.ys.peony.model.YWDJPrepareModel;
import com.ys.peony.utils.CommonUtils;
import com.ys.peony.utils.DateUtils;
import com.ys.peony.utils.JSONUtil;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.LogUtil;
import com.ys.peony.utils.NetUtils;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.StringUtils;
import com.ys.peony.utils.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 查看借款人信息
 * Created by yml on 17/5/30.
 */

public class BusiInfoActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.back_btn)
    RelativeLayout backBtn;

    @BindView(R.id.title_name_tv)
    TextView titleNameTv;

    @BindView(R.id.name_value_tv)
    TextView nameTv;
    @BindView(R.id.sex_value_tv)
    TextView sexTv;
    @BindView(R.id.idcard_value_tv)
    TextView idcardTv;
    @BindView(R.id.marriage_value_tv)
    TextView marriageTv;
    @BindView(R.id.mobile_value_tv)
    TextView mobileTv;
    @BindView(R.id.domicile_value_tv)
    TextView domicileTv;
    @BindView(R.id.company_value_tv)
    TextView companyTv;
    @BindView(R.id.hasproperty_value_tv)
    TextView hasPropertyTv;
    @BindView(R.id.propertystatus_value_tv)
    TextView propertyStatusTv;
    @BindView(R.id.clientclassify_value_tv)
    TextView clientClassifyTv;

    @BindView(R.id.dyw_ll)
    LinearLayout dywLL;

    private String loanId;
    private BusiLoanModel busiLoanModel;
    private ClientInfoModel clientInfoModel;
    private List<VehicleModel> list;


    private List<YWDJPrepareItemModel> sexList;
    private List<YWDJPrepareItemModel> marriageList;
    private List<YWDJPrepareItemModel> hasPropertyList;
    private List<YWDJPrepareItemModel> propertyStatusList;
    private List<YWDJPrepareItemModel> vehicleOwnershopTagList;

    @Override
    protected int getContentResId() {
        return R.layout.activity_busiinfo;
    }

    @Override
    protected void initView() {
        titleNameTv.setText(getResources().getString(R.string.jiekuanren_info_lable));
        backBtn.setOnClickListener(this);

        Bundle bd = getIntent().getExtras();
        if (bd != null) {
            loanId = bd.getString("loanId");
        }
        getBusiInfoModel();
        getDYWList();
    }

    private void getBusiInfoModel() {
        if (!NetUtils.isNetworkConnected(this)) {
            ToastUtils.showToast(this, getResources().getString(R.string.no_net_error));
            return;
        }
        LoadingProgressUtils.showProgressDialog(this);
        JCHttpManagerUtils httpManager = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Long.parseLong(SharePreferencesUtils.getInstance().getUserModel().getId()));
        params.put("loanId", loanId);

        httpManager.get(HttpURL.ywdjSee, params, new BaseRespCallBack(BusiInfoActivity.this) {

            public void onRespSuccess(String respStr) {
                YWDJPrepareModel resp = (YWDJPrepareModel) JSONUtil.parseObject(respStr, YWDJPrepareModel.class);
                sexList = resp.getClientSexList();
                marriageList = resp.getClientMarriageList();
                hasPropertyList = resp.getHavePropertyRight();
                propertyStatusList = resp.getClientPropertyRightStatusList();
                vehicleOwnershopTagList = resp.getVehicleOwnershopTagList();
                busiLoanModel = resp.getBusiLoanVo();
                clientInfoModel = busiLoanModel.getBusiClientMain();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initBusiInfoView();
                    }
                });

            }
        });
    }

    private void getDYWList() {
        if (!NetUtils.isNetworkConnected(this)) {
            ToastUtils.showToast(this, getResources().getString(R.string.no_net_error));
            return;
        }
        LoadingProgressUtils.showProgressDialog(this);
        JCHttpManagerUtils httpManager = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", Long.parseLong(SharePreferencesUtils.getInstance().getUserModel().getId()));
        params.put("loanId", loanId);

        httpManager.get(HttpURL.mortgageSee, params, new BaseRespCallBack(BusiInfoActivity.this) {

            public void onRespSuccess(String respStr) {
                final YWDJPrepareModel resp = (YWDJPrepareModel) JSONUtil.parseObject(respStr, YWDJPrepareModel.class);
                if (resp.getBusiMortgageVo() == null) {
                    return;
                }
                list = resp.getBusiMortgageVo().getVehicleList();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < list.size(); i++) {
                            addVehicleView(list.get(i));
                        }
                    }
                });
            }

            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                ToastUtils.showToast(BusiInfoActivity.this, msg);
            }
        });
    }

    /**
     *
     */
    private void initBusiInfoView() {
        nameTv.setText(clientInfoModel.getName());
        sexTv.setText(CommonUtils.getTextbyVal(clientInfoModel.getSex(), sexList));
        idcardTv.setText(clientInfoModel.getId_card());
        marriageTv.setText(CommonUtils.getTextbyVal(clientInfoModel.getMarriage(), marriageList));
        mobileTv.setText(clientInfoModel.getMobile());
        domicileTv.setText(clientInfoModel.getFull_name());
        companyTv.setText(clientInfoModel.getCompany());
        hasPropertyTv.setText(CommonUtils.getTextbyVal(clientInfoModel.getHave_property_right(), hasPropertyList));
        propertyStatusTv.setText(CommonUtils.getTextbyVal(clientInfoModel.getProperty_right_status(), propertyStatusList));
        clientClassifyTv.setText(clientInfoModel.getClient_clas_name());
    }

    /**
     * 添加抵押物信息
     */
    private void addVehicleView(VehicleModel item) {
        RelativeLayout dywItemView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.dyw_detail_item, null);
        TextView branchTV = (TextView) dywItemView.findViewById(R.id.branch_tv);
        TextView modelTV = (TextView) dywItemView.findViewById(R.id.model_tv);
        TextView configureTV = (TextView) dywItemView.findViewById(R.id.configure_tv);
        TextView carNumTV = (TextView) dywItemView.findViewById(R.id.number_tv);
        TextView buyTimeTV = (TextView) dywItemView.findViewById(R.id.buytime_tv);
        TextView distanceTv = (TextView) dywItemView.findViewById(R.id.distance_tv);
        TextView ownerShipTagTv = (TextView) dywItemView.findViewById(R.id.ownership_tag_tv);
        TextView ownerShipPersonTv = (TextView) dywItemView.findViewById(R.id.ownership_person_tv);

        branchTV.setText(item.getBrand_name());
        modelTV.setText(item.getVehicle_type());
        configureTV.setText(item.getVehicle_grade());
        carNumTV.setText(item.getPlate_number());
        buyTimeTV.setText(DateUtils.formatTimeToStr(item.getTime_buying().getTime(), DateUtils.formatDate));
        distanceTv.setText(item.getKilometer() + "");
        ownerShipTagTv.setText(CommonUtils.getTextbyVal(item.getOwnership_tag(), vehicleOwnershopTagList));
        ownerShipPersonTv.setText(StringUtils.trimNull(item.getOwnership_person()));

        dywLL.addView(dywItemView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
        }
    }
}
